package sky.wicket;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;
import sky.domain.Airport;
import sky.domain.Flight;
import sky.service.AirportService;
import sky.tro.FlightLayer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 26.08.13
 * Time: 12:00
 * To change this template use File | Settings | File Templates.
 */
public class BasePage extends WebPage {

    @SpringBean
    AirportService airportService;

    public BasePage() {
        add(new BookmarkablePageLink("logo", HomePage.class));
        add(new BookmarkablePageLink("index", HomePage.class));
        add(new BookmarkablePageLink("findFlight", FindFlight.class));
        add(new BookmarkablePageLink("airportLink", AirportList.class));
        add(new BookmarkablePageLink("cityLink", CityList.class));
        add(new Label("currentFlight", "Minsk"));

        //код получающий список рейсов прилета и отлета в/из аэропорт
        Airport airport = airportService.getAirportByName("Minsk");
        SimpleDateFormat formatter1 = new SimpleDateFormat("dd.MM.yyyy");
        ArrayList<FlightLayer> flightList = new ArrayList<FlightLayer>();
        for(Flight flight: airport.getFlightsIn()) {
            if (formatter1.format(new Date()).equals(formatter1.format(flight.getDestinationDate()))) flightList.add(new FlightLayer(flight.getDestinationDate(),
                    "Arrive", flight.getDepartureAirport().getName()));
        }
        for(Flight flight: airport.getFlightsOut()) {
            if (formatter1.format(new Date()).equals(formatter1.format(flight.getDepartureDate()))) flightList.add(new FlightLayer(flight.getDepartureDate(),
                    "Depart", flight.getDestinationAirport().getName()));
        }
        Collections.sort(flightList, new Comparator<FlightLayer>() {
            public int compare(FlightLayer o1, FlightLayer o2) {
                return o1.getDate().compareTo(o2.getDate());
            }
        });

        ListView<FlightLayer> listView = new ListView<FlightLayer>("sheduleFlights", flightList) {
            @Override
            protected void populateItem(ListItem<FlightLayer> listItem) {
                FlightLayer flightLayer = listItem.getModelObject();
                listItem.add(new Label("sheduleStatus", flightLayer.getStatus()));
                listItem.add(new Label("sheduleDate", new SimpleDateFormat("HH:mm").format(flightLayer.getDate())));
                listItem.add(new Label("sheduleAirportName", flightLayer.getAirportName()));
            }
        };

        add(listView);
    }

}
